<?php

namespace App\Admin\Controllers\exam;

use App\Admin\Controllers\exam\actions\ImportQuestion;
use App\Common\statement\CommonStatement;
use App\Models\exams\Question;
use App\Models\User;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class QuestionController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '试题';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Question());

        $grid->model()->latest();
        $grid->disableExport();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
        });

        $grid->column('id', __('ID'));
        $grid->column('title', __('Title'));
        $grid->column('cate', __('Cate'))->display(function ($cate){
            return CommonStatement::question_cate[$cate];
        });
        $grid->column('score', __('分值'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportQuestion());
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Question::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('cate_id', __('Cate'));
        $show->field('type', __('Type'));
        $show->field('title', __('Title'));
        $show->field('content', __('Content'))->editor();
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Question());
        $form->text('title', __('Title'))->required();
        $form->select('cate',__('分类'))->options([1=>'党史',2=>'文史'])->default(1);
        $form->select('type',__('题型'))->options([1=>'单选',2=>'多选'])
            ->when(1, function (Form $form) {
                $form->radioCard('answer',__('答案'))->options(['A'=>'A','B'=>'B','C'=>'C','D'=>'D','E'=>'E','F'=>'F']);
            })
            ->when(2,function (Form $form){
                $form->checkboxCard('answer',__('答案'))->options(['A'=>'A','B'=>'B','C'=>'C','D'=>'D','E'=>'E','F'=>'F']);
            });
        $form->datetime('question_date',__('出题时间'))->default(Carbon::now());
        $form->select('user_id',__('出题人'))->options(User::pluck('name','id'));
        $form->text('A',__('A选项'));
        $form->text('B',__('B选项'));
        $form->text('C',__('C选项'));
        $form->text('D',__('D选项'));
        $form->text('E',__('E选项'));
        $form->text('F',__('F选项'));
//        $form->checkboxCard('answer',__('答案'))->options(['A'=>'A','B'=>'B','C'=>'C','D'=>'D','E'=>'E','F'=>'F']);
        $form->decimal('score',__('分值'))->required();
        $form->editor('analysisi');
        $form->saving(function (Form $form) {

        });

        return $form;
    }
}
